package com.rdxer.api;

import com.rdxer.api.para.IdsPara;
import com.rdxer.api.para.IdsStringListPara;
import com.rdxer.api.para.IdsStringPara;
import com.rdxer.api.para.SaveAccountPara;
import com.rdxer.dataview.AccountDV;
import com.rdxer.model.Account;
import com.rdxer.core.security.AppUserDetails;
import com.rdxer.core.security.CheckService;
import com.rdxer.core.entity.Result;
import com.rdxer.core.entity.StatusCode;
import com.rdxer.core.utils.search.SearchPara;
import com.rdxer.core.exception.exceptions.BaseException;
import com.rdxer.service.AccountManagerService;
import com.rdxer.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/userManagement")
public class AccountManageApi {

    @Resource
    AccountService accountService;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    private CheckService checkService;
    @Autowired
    private AccountManagerService accountManagerService;


    //	 * 分页+多条件查询
    @PostMapping("/getList")
    public Result<Page<AccountDV>> search(@Valid @RequestBody SearchPara para) {

        Result<Page<AccountDV>> result = new Result<>();
        Page<Account> data = accountManagerService.search(para);
        Page<AccountDV> dataDV = accountManagerService.accountPageToDVPage(data);
        result.setData(dataDV);
        return result;
    }

    /**
     * 启用
     */
    @PutMapping("/enable")
    public Result<?> enable(@RequestBody IdsStringListPara para) {
        accountManagerService.enableAllByCheckPerms(para.getIds());
        return new Result(true, StatusCode.OK, "启用成功");
    }

    /**
     * 禁用
     */
    @PutMapping("/disable")
    public Result<?> disable(@RequestBody IdsStringListPara para) {
        accountManagerService.disableAllByCheckPerms(para.getIds());
        return new Result(true, StatusCode.OK, "禁用成功");
    }

    /**
     * 保存对象   有id 保存，  无id 创建对象
     */
    @PostMapping("/save")
    public Result<?> save(@RequestBody SaveAccountPara para) {

        accountManagerService.save(para);

        return new Result(true, StatusCode.OK, "操作成功");
    }
}
